package com.bejoy.coolatin;

import java.io.File;

import android.app.Application;
import android.graphics.Bitmap.CompressFormat;

import com.baidu.mapapi.SDKInitializer;
import com.bejoy.coolatin.proxy.CrashHandler;
import com.bejoy.coolatin.volley.ImageCacheManager;
import com.bejoy.coolatin.volley.RequestManagers;
import com.bejoy.coolatin.volley.ImageCacheManager.CacheType;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

/**
 * Example application for adding an L1 image cache to Volley. 全局文件
 * 
 * @author Savvy 2014.12.01
 */
public class MainApplication extends Application {

	private static int DISK_IMAGECACHE_SIZE = 1024 * 1024 * 10;
	private static CompressFormat DISK_IMAGECACHE_COMPRESS_FORMAT = CompressFormat.PNG;
	private static int DISK_IMAGECACHE_QUALITY = 100; // PNG is lossless so
														// quality is ignored
														// but must be provided
														// private int
														// versionsType =2;//
														// 版本类型：1客户，2设计师，3工长，4监理（正式版本需要写死）
	private int refreshTime = 5000;// 聊天接口刷新间隔时间
	private boolean isRunning = true;

	// private boolean loginFlag=false;//登录状态

	@Override
	public void onCreate() {
		super.onCreate();
		// 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
		SDKInitializer.initialize(getApplicationContext());
		init();
		File cacheDir = StorageUtils.getCacheDirectory(this);
		ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
				this)
				// 添加你的配置需求
				.threadPoolSize(2)
				.threadPriority(2)
				.diskCacheExtraOptions(480, 800, null)
				.diskCache(new UnlimitedDiscCache(cacheDir))
				// default
				.diskCacheSize(50 * 1024 * 1024).diskCacheFileCount(60)
				.tasksProcessingOrder(QueueProcessingType.FIFO)
				.denyCacheImageMultipleSizesInMemory()
				.memoryCache(new LruMemoryCache(5 * 1024 * 1024))
				.memoryCacheSize(2 * 1024 * 1024).memoryCacheSizePercentage(13)
				.imageDownloader(new BaseImageDownloader(this))
				.imageDecoder(new BaseImageDecoder(false))
				.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
				.build();
		ImageLoader.getInstance().init(configuration);

	}

	// public int getVersionsType() {
	// return versionsType;
	// }
	//
	// public void setVersionsType(int versionsType) {
	// this.versionsType = versionsType;
	// }

	public int getRefreshTime() {
		return refreshTime;
	}

	// public void setRefreshTime(int refreshTime) {
	// this.refreshTime = refreshTime;
	// }

	public boolean isRunning() {
		return isRunning;
	}

	public void setRunning(boolean isRunning) {
		this.isRunning = isRunning;
	}

	/**
	 * Intialize the request manager and the image cache
	 */
	private void init() {
		// 注册crashHandler
		CrashHandler crashHandler = CrashHandler.getInstance();
		crashHandler.init(getApplicationContext());
		RequestManagers.init(this);
		createImageCache();
	}

	/**
	 * Create the image cache. Uses Memory Cache by default. Change to Disk for
	 * a Disk based LRU implementation.
	 */
	private void createImageCache() {
		ImageCacheManager.getInstance().init(this, this.getPackageCodePath(),
				DISK_IMAGECACHE_SIZE, DISK_IMAGECACHE_COMPRESS_FORMAT,
				DISK_IMAGECACHE_QUALITY, CacheType.MEMORY);
	}

}